import { observable, action, runInAction, makeAutoObservable } from 'mobx'
import { Storage, SeStorage } from '@/assets/js/storage';
import { LOGIN_USERINFO, SETTING, TOKEN, NOTICE_LIST, NOTICE_FRIST, IMG_URL, MESSAGE_LIST } from '@/utils/constant'
import settings from '@/config'
import history from '@/utils/history'

class AppStore{
  constructor() {
    makeAutoObservable(this)
  }
  /* state */
  @observable token = Storage.get(TOKEN) || {};
  @observable loginUserInfo = Storage.get(LOGIN_USERINFO) || {};
  @observable settings = Storage.get(SETTING) || settings;
  @observable chatList = Storage.get(MESSAGE_LIST) || [];
  @observable pageLoad = Date.now();
  @observable logoTitle = process.env.REACT_APP_TAB_TITLE;
  @observable noticeList = Storage.get(NOTICE_LIST) || {};
  @observable noticeFrist = Storage.get(NOTICE_FRIST) || 0;
  @observable imgUrl = Storage.get(IMG_URL) || '';
  /* mutations */
  // 设置登录信息
  @action saveLoginUserinfo = (state) => {  
    runInAction(() => {
      this.loginUserInfo = state;
      Storage.set(LOGIN_USERINFO, state)
    })
  }
  // 设置公告列表
  @action saveImgUrl = (state) => {  
    runInAction(() => {
      this.imgUrl = state;
      Storage.set(IMG_URL, state)
    })
  }
  // 设置聊天室历史记录
  @action changeMessage = (state) => {  
    runInAction(() => {
      this.chatList = state;
      Storage.set(MESSAGE_LIST, state)
    })
  }
  // 添加聊天室历史记录
  @action addMessage = (state) => {  
    runInAction(() => {
      let newList = JSON.parse(JSON.stringify(this.chatList));
      let dataLength = newList.length;
      let index = newList.findIndex((item) => item.uuid == state.uuid);
      if (index === -1) {
        // 群聊消息超过300
        if (dataLength >= 300) {
          newList.splice(0, 100);
        }
        // 内容一样时
        let idx = newList.findIndex((item) => item.content == state.content);
        // 时间一样时
        let tidx = newList.findIndex((item) => item.time == state.time);
        //内容不一样时
        if (idx === -1) {
          newList.push(state);
        } else {
          // 内容一样时，时间不一样才push
          if (tidx === -1) {
            newList.push(state);
          }
        }
      } else {
        newList[index] = state;
      }
      this.chatList = newList;
      Storage.set(MESSAGE_LIST, newList);
    })
  }
  // 设置公告列表
  @action saveNoticeList = (state) => {  
    runInAction(() => {
      this.noticeList = state;
      Storage.set(NOTICE_LIST, state)
    })
  }
  // 设置公告刷新显示
  @action saveNoticeFrist = (state) => {  
    runInAction(() => {
      this.noticeFrist = state;
      Storage.set(NOTICE_FRIST, state)
    })
  }
  // 设置token
  @action saveToken = (state) => {  
    this.token = state;
    Storage.set(TOKEN, state)
  }
  //页面刷新
  @action setPageLoad = () => this.pageLoad = Date.now(); 
  // 设置
  @action setSettings = (key, value) => { 
    this.settings[key] = value
    Storage.set(SETTING, this.settings);
    // 设置主题
    document.documentElement.setAttribute("theme", this.settings.theme);
  }
  // 退出登录
  @action logout = () => {
    Storage.clear()
    SeStorage.clear()
    history.push('/login')
    window.location.reload()
  }

  /* action */
  // 获取登录信息
  @action getLoginUserInfo = async (payload) => { 
    runInAction()
  }
}
const AppStores = new AppStore()
export default AppStores